package com.idione.controller;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.idione.activities.R;
import com.idione.model.StructQuestion;
import com.idione.util.LogUtil;

public class ShowAnswer {

	private StructQuestion question;
	private Context context;
	private int numbQuestion;
	
	private TextView TVquestionText;
	private ImageView IVquestionImg;
	private TextView TVargumenText;
	private ImageView IVargumentImg;
	private TextView alternativeCorrectText;
	private ImageView alternativeCorrectImg;
	private Button button;
	private TextView lblViewAnswerText;
	private TextView lblViewAlternativeCorrect;
	private TextView lblViewTitle;
	
	private Dialog dialog;
	
	public ShowAnswer(StructQuestion question, Context context, int numbQuestion) {
		this.question = question;
		this.context = context;
		this.numbQuestion = numbQuestion;
		
		this.prepareDialog();
	}

	public void show() {
		this.log("show()");

		//now that the dialog is set up, it's time to show it    
		dialog.show();
	}
	
	private void prepareDialog() {
		//set up dialog
		dialog = new Dialog(context, R.style.DialogTheme);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.answer);

		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(false);
		dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation;
		
		this.findViewByIdFromActivity();
		
		//set up questionNumber
		lblViewTitle.setText(context.getString(R.string.LblTextView) + " " + numbQuestion);
		
		//set up questionText
		TVquestionText.setText(question.getQuestionText());

		//set up imageQuestion
		if (question.getQuestionImage(context) != null)
			IVquestionImg.setImageDrawable(question.getQuestionImage(context));
		else
			IVquestionImg.setVisibility(View.GONE);

		//set up argumentText
		TVargumenText.setText(question.getArgumentText());

		//set up argumentTextImage
		if (question.getArgumentImage(context) != null)
			IVargumentImg.setImageDrawable(question.getArgumentImage(context));
		else
			IVargumentImg.setVisibility(View.GONE);

		//set up alternativeCorrectText
		alternativeCorrectText.setText(question.getAnswerText());

		//set up alternativeCorrectImage
		if (question.getAnswerImage(context) != null)
			alternativeCorrectImg.setImageDrawable(question.getAnswerImage(context));
		else
			alternativeCorrectImg.setVisibility(View.GONE);

		//set up button
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		this.setTypeFace();
		
		dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
	}
	
	private void findViewByIdFromActivity() {
		TVquestionText = (TextView) dialog.findViewById(R.id.LblViewQuestionText);
		IVquestionImg = (ImageView) dialog.findViewById(R.id.ImgViewQuestion);
		TVargumenText = (TextView) dialog.findViewById(R.id.LblViewArgument);
		IVargumentImg = (ImageView) dialog.findViewById(R.id.ImgViewArgument);
		alternativeCorrectText = (TextView) dialog.findViewById(R.id.LblAlternativeCorrectText);
		alternativeCorrectImg = (ImageView) dialog.findViewById(R.id.ImgViewAlternativeCorrect);
		button = (Button) dialog.findViewById(R.id.BtnOk);
		
		lblViewTitle = (TextView) dialog.findViewById(R.id.LblViewTitle);
		lblViewAnswerText = (TextView) dialog.findViewById(R.id.LblViewAnswerText);
		lblViewAlternativeCorrect = (TextView) dialog.findViewById(R.id.LblViewAlternativeCorrect);
	}
	
	private void setTypeFace() {
		Typeface tfb = LogUtil.getTypeFaceButton(context);
		Typeface tfs = LogUtil.getTypeFaceSubtitle(context);
		Typeface tfp = LogUtil.getTypeFaceParagraph(context);

		TVquestionText.setTypeface(tfp);
		TVargumenText.setTypeface(tfp);
		alternativeCorrectText.setTypeface(tfp);
		button.setTypeface(tfb);
		
		lblViewTitle.setTypeface(tfs);
		lblViewAnswerText.setTypeface(tfs);
		lblViewAlternativeCorrect .setTypeface(tfs);
	}
	
	/*--------------------------------------------------------------------------*
	 * Log																		*
	 *--------------------------------------------------------------------------*/
	private final String LOGTAG = "LogsClass " + this.getClass().getName();
	private void log(String method) {
		if (LogUtil.controllerLog)
			Log.i(LOGTAG, "... method : " + this.getClass().getSimpleName() + "." + method);
	}
}
